package test;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseInsertUser {
    public static void main(String[] args) {
        try {
            // 创建HBase配置
            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");
            conf.set("hbase.zookeeper.property.clientPort", "2181"); // 默认端口是2181

            // 创建HBase连接
            Connection connection = ConnectionFactory.createConnection(conf);

            // 向用户表插入三条数据
            Table userTable = connection.getTable(TableName.valueOf("lsj0111_users"));

            Put put1 = new Put(Bytes.toBytes("user1"));
            put1.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("罗仕佳"));
            put1.addColumn(Bytes.toBytes("info"), Bytes.toBytes("email"), Bytes.toBytes("luo@example.com"));

            Put put2 = new Put(Bytes.toBytes("user2"));
            put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zz"));
            put2.addColumn(Bytes.toBytes("info"), Bytes.toBytes("email"), Bytes.toBytes("z@example.com"));

            Put put3 = new Put(Bytes.toBytes("user3"));
            put3.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("xx"));
            put3.addColumn(Bytes.toBytes("info"), Bytes.toBytes("email"), Bytes.toBytes("x@example.com"));

            userTable.put(put1);
            userTable.put(put2);
            userTable.put(put3);

            System.out.println("成功插入三条数据到lsj0111_users表中");

            // 关闭用户表
            userTable.close();

            // 关闭连接
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
